"""
计算给定二叉树的所有左叶子之和。

示例：

    3
   / \
  9  20
    /  \
   15   7

在这个二叉树中，有两个左叶子，分别是 9 和 15，所以返回 24
"""
from leetcode_test import TreeNode


class Solution:
    """
    """
    def sumOfLeftLeaves(self, root: TreeNode) -> int:
        
        def helper(root: TreeNode, is_left: bool):
            if not root:
                return 0

            if not root.left and not root.right and is_left:
                return root.val
            
            return helper(root.left, True) +  helper(root.right, False)
        return helper(root, False)

def test2():
    t = TreeNode.create([3, 9, 20, None, None, 15, 7])
    # 9 + 15
    assert Solution().sumOfLeftLeaves(t) == 24

def test1():
    t = TreeNode.create([1, 2, 3])
    assert Solution().sumOfLeftLeaves(t) == 2

def test3():
    t= TreeNode.create([1,2,3,4,5])
    assert Solution().sumOfLeftLeaves(t) == 4

if __name__ == "__main__":
    t = TreeNode.create([3, 9, 20, None, None, 15, 7])
    # 9 + 15
    print(Solution().sumOfLeftLeaves(t))